package com.zbkc.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.zbkc.bean.LoginBean;
import com.zbkc.constant.Constant;
import com.zbkc.interfaces.SCInterface;
import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.AlertUtils;
import com.zbkc.util.BASE64Util;
import com.zbkc.util.MyApplication;
import com.zbkc.util.PreferenceUtils;
import com.zbkc.util.StarCloudUtil;
import com.zbkc.util.ThreadPoolFactory;

public class LoginActivity extends BaseTitleActivity implements
		View.OnClickListener {

	private EditText etPwd;
	private EditText etUserName;

	private LoginBean loginBean;

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				if (loginBean != null) {
					String str = loginBean.getOtherMessage().trim();
					if (str.equals(Constant.SUCCESS)) {
						PreferenceUtils.putString(Constant.USERNAME, etUserName
								.getText().toString().trim());
						PreferenceUtils.putString(Constant.PASSWORD, etPwd
								.getText().toString().trim());
						Intent localIntent = new Intent();
						localIntent.setClass(LoginActivity.this,
								TabManagerActivity.class);
						LoginActivity.this.startActivity(localIntent);
					} else {
						AlertUtils.toast(LoginActivity.this, str);
					}
				}
				AlertUtils.dismissLoadingDialog();
				break;
			}
		}
	};

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.activity_login);
		MyApplication.getInstance().addActivity(this);
		bindView();
	}

	private void bindView() {
		setTitle(getString(R.string.login));
		setBtn1Visiable(false);
		setBtn2Visiable(false);
		etUserName = ((EditText) findViewById(R.id.et_username));
		etPwd = ((EditText) findViewById(R.id.et_pwd));
		etPwd.setOnEditorActionListener(new EditorActionLisenter());
		findViewById(R.id.btn_login).setOnClickListener(this);

		String username = PreferenceUtils.getString(Constant.USERNAME);
		etUserName.setText(username);

		String password = PreferenceUtils.getString(Constant.PASSWORD);
		etPwd.setText(password);

	}

	class EditorActionLisenter implements TextView.OnEditorActionListener {

		@Override
		public boolean onEditorAction(TextView textView, int actionId,
				KeyEvent keyEvent) {
			// 使用此监听器会避免使用setOnKeyListener来监听按钮会调用两次
			if (actionId == EditorInfo.IME_ACTION_DONE) {
				// 监听到软盘上的完成按键
				loginGo();
				return true;
			}
			return false;
		}
	}

	private void loginGo() {
		String username = this.etUserName.getText().toString().trim();
		String password = this.etPwd.getText().toString().trim();
		if (TextUtils.isEmpty(username)) {
			AlertUtils.toast(this,
					getResources().getString(R.string.null_username));
			return;
		}
		if (TextUtils.isEmpty(password)) {
			AlertUtils.toast(this,
					getResources().getString(R.string.null_password));
			return;
		}
		username = BASE64Util.encode(username.getBytes());
		password = BASE64Util.encode(password.getBytes());
		PreferenceUtils.putString("username", username);
		PreferenceUtils.putString("password", password);
		AlertUtils.showLoadingDialog(this, "登录中,请稍后...", true);
		ThreadPoolFactory.getInstance().execute(new Runnable() {
			public void run() {
				loginBean = SCInterface.login("ab123", "",
						BASE64Util.encode(StarCloudUtil.getClientDeviceName()
								.getBytes()), BASE64Util.encode(StarCloudUtil
								.getClientVersion().getBytes()));
				handler.sendEmptyMessage(1);
			}
		});
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			loginGo();
			break;
		}
	}
}
